/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package gui.article;

import ejb.model.EJBModel;
import gui.renderer.CustomIconRenderer;
import java.util.ArrayList;
import javax.swing.DefaultComboBoxModel;
import javax.swing.JComboBox;
import javax.swing.JOptionPane;
import javax.swing.JTree;
import javax.swing.event.TreeSelectionEvent;
import javax.swing.event.TreeSelectionListener;
import javax.swing.tree.DefaultMutableTreeNode;
import javax.swing.tree.DefaultTreeModel;
import model.gui.MenuModel;
import util.article.GroupDetails;

/**
 *
 * @author Joris
 */
public class ArticleGroupPanel extends javax.swing.JPanel implements TreeSelectionListener {
    
    private DefaultMutableTreeNode top;
    private MenuModel model;
    private DefaultComboBoxModel<GroupDetails> groupComboBoxModel;
    private DefaultComboBoxModel<GroupDetails> editGroupComboBoxModel;
    private GroupDetails rootGroup;
    private ArrayList<GroupDetails> roots;

    /**
     * Creates new form ArticleGroupPanel
     */
    public ArticleGroupPanel(MenuModel model) {
        this.model = model;
        groupComboBoxModel = new DefaultComboBoxModel<GroupDetails>();
        editGroupComboBoxModel = new DefaultComboBoxModel<GroupDetails>();
        rootGroup = new GroupDetails(-1, "Hoofdgroep", "Hoofdgroep", -1);
        top = new DefaultMutableTreeNode(rootGroup);
        initComponents();
        groupTree.getSelectionModel().addTreeSelectionListener(this);
        getAllRootGroups();
        
        
        
        
    }
    
    private void getAllRootGroups(){
        Long t1 = System.currentTimeMillis();
        roots = new ArrayList<GroupDetails>(EJBModel.getArticleGroupBean().getAllRootGroups());
        populateGroupComboBox(groupComboBoxModel);
        populateGroupComboBox(editGroupComboBoxModel);
        createNodes(top);
        Long t2 = System.currentTimeMillis();
        model.setStatus("Collected all groups in " + (t2-t1) + " ms");
    }
    
    private void populateGroupComboBox(DefaultComboBoxModel cmbModel){
        cmbModel.removeAllElements();
        cmbModel.addElement(rootGroup);
        for(GroupDetails root:roots){
            cmbModel.addElement(root);
            if(root.getChildrenGroups() != null){
                for(GroupDetails child:root.getChildrenGroups())
                    addComboboxItem(cmbModel, child);
            }
        }   
    }
    
    private void addComboboxItem(DefaultComboBoxModel cmbModel, GroupDetails gd){
        cmbModel.addElement(gd);
        if(gd.getChildrenGroups() != null){
                for(GroupDetails childGroup:gd.getChildrenGroups())
                    addComboboxItem(cmbModel, childGroup);
            }
    }
    
    private void createNodes(DefaultMutableTreeNode top){
        top.removeAllChildren();
        for(GroupDetails root:roots){
            DefaultMutableTreeNode rootNode = new DefaultMutableTreeNode(root);
            if(root.getChildrenGroups() != null){
                for(GroupDetails child:root.getChildrenGroups())
                    addNode(rootNode, child);
            }
            top.add(rootNode);
        }
        ((DefaultTreeModel)groupTree.getModel()).reload(); 
    }
    
    private void addNode (DefaultMutableTreeNode root, GroupDetails child){
        DefaultMutableTreeNode childNode = new DefaultMutableTreeNode(child);
            if(child.getChildrenGroups() != null){
                for(GroupDetails childGroup:child.getChildrenGroups())
                    addNode(childNode, childGroup);
            }
            root.add(childNode);
    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jScrollPane1 = new javax.swing.JScrollPane();
        groupTree = new JTree(top);
        groupTree.setRootVisible(false);
        jPanel1 = new javax.swing.JPanel();
        jLabel1 = new javax.swing.JLabel();
        newGroupNameTxt = new javax.swing.JTextField();
        addGroupBtn = new javax.swing.JButton();
        newGroupParentCmb = new javax.swing.JComboBox();
        jLabel2 = new javax.swing.JLabel();
        newGroupDescriptionTxt = new javax.swing.JTextField();
        jPanel2 = new javax.swing.JPanel();
        jLabel3 = new javax.swing.JLabel();
        editGroupNameTxt = new javax.swing.JTextField();
        jLabel4 = new javax.swing.JLabel();
        editGroupDescription = new javax.swing.JTextField();
        editParentGroupCmb = new javax.swing.JComboBox();
        editGroupBtn = new javax.swing.JButton();
        deleteGroupBtn = new javax.swing.JButton();

        groupTree.setBorder(javax.swing.BorderFactory.createEmptyBorder(20, 20, 20, 20));
        groupTree.setFont(new java.awt.Font("Tahoma", 0, 14)); // NOI18N
        groupTree.setModel(new DefaultTreeModel(top));
        groupTree.setCellRenderer(new CustomIconRenderer());
        groupTree.setRowHeight(20);
        jScrollPane1.setViewportView(groupTree);

        jPanel1.setBorder(javax.swing.BorderFactory.createTitledBorder(null, "Nieuwe groep toevoegen", javax.swing.border.TitledBorder.DEFAULT_JUSTIFICATION, javax.swing.border.TitledBorder.DEFAULT_POSITION, new java.awt.Font("Tahoma", 0, 14))); // NOI18N
        jPanel1.setPreferredSize(new java.awt.Dimension(286, 195));

        jLabel1.setFont(new java.awt.Font("Tahoma", 0, 14)); // NOI18N
        jLabel1.setText("Naam");

        newGroupNameTxt.setFont(new java.awt.Font("Tahoma", 0, 14)); // NOI18N

        addGroupBtn.setFont(new java.awt.Font("Tahoma", 0, 14)); // NOI18N
        addGroupBtn.setText("Groep toevoegen");
        addGroupBtn.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                addGroupBtnActionPerformed(evt);
            }
        });

        newGroupParentCmb.setFont(new java.awt.Font("Tahoma", 0, 14)); // NOI18N
        newGroupParentCmb.setModel(groupComboBoxModel);

        jLabel2.setFont(new java.awt.Font("Tahoma", 0, 14)); // NOI18N
        jLabel2.setText("Omschrijving");

        newGroupDescriptionTxt.setFont(new java.awt.Font("Tahoma", 0, 14)); // NOI18N

        javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
        jPanel1.setLayout(jPanel1Layout);
        jPanel1Layout.setHorizontalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(newGroupNameTxt)
                    .addComponent(addGroupBtn, javax.swing.GroupLayout.DEFAULT_SIZE, 254, Short.MAX_VALUE)
                    .addComponent(newGroupParentCmb, 0, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(jLabel1)
                            .addComponent(jLabel2))
                        .addGap(0, 0, Short.MAX_VALUE))
                    .addComponent(newGroupDescriptionTxt))
                .addContainerGap())
        );
        jPanel1Layout.setVerticalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addComponent(jLabel1)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(newGroupNameTxt, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addComponent(jLabel2)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(newGroupDescriptionTxt, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(8, 8, 8)
                .addComponent(newGroupParentCmb, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(addGroupBtn)
                .addGap(18, 18, 18))
        );

        jPanel2.setBorder(javax.swing.BorderFactory.createTitledBorder(null, "Huidige groep bewerken", javax.swing.border.TitledBorder.DEFAULT_JUSTIFICATION, javax.swing.border.TitledBorder.DEFAULT_POSITION, new java.awt.Font("Tahoma", 0, 14))); // NOI18N

        jLabel3.setFont(new java.awt.Font("Tahoma", 0, 14)); // NOI18N
        jLabel3.setText("Naam");

        editGroupNameTxt.setFont(new java.awt.Font("Tahoma", 0, 14)); // NOI18N

        jLabel4.setFont(new java.awt.Font("Tahoma", 0, 14)); // NOI18N
        jLabel4.setText("Omschrijving");

        editGroupDescription.setFont(new java.awt.Font("Tahoma", 0, 14)); // NOI18N

        editParentGroupCmb.setFont(new java.awt.Font("Tahoma", 0, 14)); // NOI18N
        editParentGroupCmb.setModel(editGroupComboBoxModel);

        editGroupBtn.setFont(new java.awt.Font("Tahoma", 0, 14)); // NOI18N
        editGroupBtn.setText("Groep aanpassen");
        editGroupBtn.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                editGroupBtnActionPerformed(evt);
            }
        });

        deleteGroupBtn.setFont(new java.awt.Font("Tahoma", 0, 14)); // NOI18N
        deleteGroupBtn.setText("Groep verwijderen");
        deleteGroupBtn.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                deleteGroupBtnActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout jPanel2Layout = new javax.swing.GroupLayout(jPanel2);
        jPanel2.setLayout(jPanel2Layout);
        jPanel2Layout.setHorizontalGroup(
            jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel2Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(editGroupNameTxt)
                    .addComponent(editGroupDescription)
                    .addComponent(editParentGroupCmb, 0, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addComponent(editGroupBtn, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addGroup(jPanel2Layout.createSequentialGroup()
                        .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(jLabel3)
                            .addComponent(jLabel4))
                        .addGap(0, 0, Short.MAX_VALUE))
                    .addComponent(deleteGroupBtn, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                .addContainerGap())
        );
        jPanel2Layout.setVerticalGroup(
            jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel2Layout.createSequentialGroup()
                .addGap(7, 7, 7)
                .addComponent(jLabel3)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(editGroupNameTxt, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jLabel4)
                .addGap(4, 4, 4)
                .addComponent(editGroupDescription, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(editParentGroupCmb, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(editGroupBtn)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addComponent(deleteGroupBtn))
        );

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
        this.setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGap(20, 20, 20)
                .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 489, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(18, 18, 18)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                    .addComponent(jPanel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addComponent(jPanel2, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGap(28, 28, 28)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                    .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 414, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                        .addComponent(jPanel2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)))
                .addGap(25, 25, 25))
        );
    }// </editor-fold>//GEN-END:initComponents

    private void addGroupBtnActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_addGroupBtnActionPerformed
        GroupDetails selected = (GroupDetails)groupComboBoxModel.getSelectedItem();
        GroupDetails gd = new GroupDetails(-1, newGroupNameTxt.getText(), newGroupDescriptionTxt.getText(), selected.getId());
        EJBModel.getArticleGroupBean().createGroup(gd);
        getAllRootGroups();
    }//GEN-LAST:event_addGroupBtnActionPerformed

    private void deleteGroupBtnActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_deleteGroupBtnActionPerformed
        if(JOptionPane.showConfirmDialog(this, "Ben je zeker dat je deze groep wilt verwijderen?", "Waarschuwing", JOptionPane.YES_NO_OPTION) == 0){
            GroupDetails selected = (GroupDetails)((DefaultMutableTreeNode) groupTree.getLastSelectedPathComponent()).getUserObject();
            EJBModel.getArticleGroupBean().removeGroup(selected);
            getAllRootGroups();
        }
    }//GEN-LAST:event_deleteGroupBtnActionPerformed

    private void editGroupBtnActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_editGroupBtnActionPerformed
        GroupDetails selected = (GroupDetails)((DefaultMutableTreeNode) groupTree.getLastSelectedPathComponent()).getUserObject();
        GroupDetails selectedParent = (GroupDetails)editGroupComboBoxModel.getSelectedItem();
        selected.setName(editGroupNameTxt.getText());
        selected.setDescription(editGroupDescription.getText());
        selected.setParentGroup(selectedParent.getId());
        EJBModel.getArticleGroupBean().editGroup(selected);
        getAllRootGroups();
    }//GEN-LAST:event_editGroupBtnActionPerformed

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton addGroupBtn;
    private javax.swing.JButton deleteGroupBtn;
    private javax.swing.JButton editGroupBtn;
    private javax.swing.JTextField editGroupDescription;
    private javax.swing.JTextField editGroupNameTxt;
    private javax.swing.JComboBox editParentGroupCmb;
    private javax.swing.JTree groupTree;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JPanel jPanel2;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JTextField newGroupDescriptionTxt;
    private javax.swing.JTextField newGroupNameTxt;
    private javax.swing.JComboBox newGroupParentCmb;
    // End of variables declaration//GEN-END:variables

    @Override
    public void valueChanged(TreeSelectionEvent e) {
        if(groupTree.getSelectionCount() > 0){
        Object[] selected = ((DefaultMutableTreeNode) groupTree.getLastSelectedPathComponent()).getUserObjectPath();
            groupComboBoxModel.setSelectedItem(selected[selected.length-1]);
            editGroupComboBoxModel.setSelectedItem(selected[selected.length-2]);
            editGroupNameTxt.setText(((GroupDetails)selected[selected.length-1]).getName());
            editGroupDescription.setText(((GroupDetails)selected[selected.length-1]).getDescription());
        }
    }
}
